package com.xtjc03.domain.service.impl;


import com.xtjc03.domain.entity.Led;
import com.xtjc03.domain.service.LedService;
import com.xtjc03.serial.service.SerialPortServiceImpl;
import com.xtjc03.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class LedServiceImpl implements LedService {

    private static final String COMPANY = "xtjc03";

    @Autowired
    TimeUtil timeUtil;

    @Autowired
    SerialPortServiceImpl serialPortService;

    @Override
    public Led start() {
        return getComMsg("打开Led", "led0");
    }

    @Override
    public Led stop() {
        return getComMsg("关闭Led", "led1");
    }

    @Override
    public Led read() {
        return getComMsg("读取Led状态", "led2");
    }

    public Led getComMsg(String msg, String cmd) {
        Led led = new Led();
        led.setCmdStatus(msg);
        String s = serialPortService.sendStringToCom("xtjc03led", COMPANY + cmd + "\r\n");
        if (s != null) {
            led.setDeviceStatus(s.substring(9).startsWith("1") ? "开" : "关");
            led.setCurrentDate(timeUtil.formatDate(new Date()));
        }
        return led;
    }
}
